<%-- 
    Document   : createevent
    Created on : Apr 11, 2009, 11:43:36 AM
    Author     : marc
--%>
<%!
    public boolean timeIsValid(String time) {
        String fixedTime = time.toUpperCase().replace(" ", "");

        // can accept
        //  numbers  = hour in military
        //  numbers:numbers = hour:min military
        //  numbers = hour in ampm
        //  numbers:numbers[am|pm|AM|PM|a|p|A|P] = hour:min in am/pm

        // are there invalid characters?
        if (time.matches("/[^0-9:APM]/") == true) {
            return false;
        }

        //TODO:  Actual validity checking for time

        return true;
    }

    public String calculateTime(String time) {

        String output = "";
        String fixedTime = time.toUpperCase().replace(" ", "");


        //TODO:  Calculate actual time from time passed

        return output;
    }


%>



<%@page contentType="text/html" pageEncoding="UTF-8" import="org.bsscheduler.*"%>
<jsp:useBean id="user" class="org.bsscheduler.User" scope="session" />

<%-- if not logged in, go to login page --%>
<% if (!user.getLoggedIn()) {%>
<jsp:forward page="index.jsp" />
<% }%>
<%



        String title = request.getParameter("title");
        String startdate = request.getParameter("startdate");
        String starttime = request.getParameter("starttime");
        String enddate = request.getParameter("enddate");
        String endtime = request.getParameter("endtime");
        String description = request.getParameter("description");
        String participants[] = request.getParameterValues("participants");

        String tempHour = "";
        String tempMinute = "";
        String tempTime = "";


        // Time MUS be in either 24-hour time with a colon
        // or as HH:MM A with the space optional and M optional
        starttime = starttime.toUpperCase().replaceAll(" ", "");
        endtime = endtime.toUpperCase().replaceAll(" ", "");

        if( (starttime.contains("A") || starttime.contains("P"))
            && (starttime.contains(":"))){
            tempHour = starttime.substring(0,starttime.indexOf(":"));
            tempMinute = starttime.substring(starttime.indexOf(":")+1,starttime.indexOf(":")+3 );

               if(starttime.contains("P") ){
                tempHour = (new Integer(Integer.parseInt(tempHour) + 12)).toString();
            }


            starttime = tempHour + ":" + tempMinute;
        }

        if( (endtime.contains("A")  || endtime.contains("P")  )
            && (endtime.contains(":"))){
            tempHour = endtime.substring(0,endtime.indexOf(":"));
            tempMinute = endtime.substring(endtime.indexOf(":")+1,endtime.indexOf(":")+3 );


            endtime = tempHour + ":" + tempMinute;
        }

        String startTimeDate = startdate + starttime;
        String endDateTime = enddate + endtime;


        Event myEvent = new Event(user.getEmail());
        myEvent.setTitle(title);
        myEvent.setStartDate(startdate + " " + starttime);
        myEvent.setEndDate(enddate + " " + endtime);
        myEvent.setDescription(description);
        if (participants != null) {
            int i;
            for (i = 0; i < participants.length; i++) {

                myEvent.addParticipant(participants[i]);
            }
        }
        user.events.addEvent(myEvent);
%>
<jsp:forward page="main.jsp" />

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>bsserver - error</title>
    </head>
    <body>
        <p>You should not be seeing this page.</p><p>Error in creating event.<p></p>Please contact system administrator.</p>


    </body>
</html>


<!--

function(value) {
// is it AM/PM?
var condensedVal = value.toString().toUpperCase().split(' ').join('');

// does response contan invalid characters
var invalidMatch = condensedVal.toString().search(/[^0-9:APM]/);
if( invalidMatch != -1){
alert("containsInvalidString");
return false;
}

var amMatchPos = condensedVal.toString().search(/AM/);
var pmMatchPos = condensedVal.toString().search(/PM/);
var colonMatch = condensedVal.toString().search(/:/);
var tHour = 0;
var tMin= 0;


// value has an AM in it
if(amMatchPos != -1){
// if there's no colon, its hours + am
if(colonMatch == -1){
tHour = Number(condensedVal.toString().substring(0,amMatchPos));
} else {
// otherwise there's a colon
tHour = Number(condensedVal.toString().substring(0,colonMatch));
tMin = Number(condensedVal.toString().substring(colonMatch+1,amMatchPos));
}

alert("A: "+tHour  + "-" + tMin.toString());
} else if (pmMatchPos != -1){
// there's a PM in it'
// if there's no colon, its hours + am
if(colonMatch == -1){
tHour = Number(condensedVal.toString().substring(0,pmMatchPos));
tHour = Number(tHour) + 12;
} else {
// otherwise there's a colon
tHour = Number(condensedVal.toString().substring(0,colonMatch));
tHour = Number(tHour) + 12;
tMin = Number(condensedVal.toString().substring(colonMatch+1,pmMatchPos));
}

} else {
// now must be military time
invalidMatch = condensedVal.toString().search(/[^0-9:]/);
if( invalidMatch != -1){
alert("containsInvalidString");
return false;
}
if(colonMatch == -1){
// no colon in it
tHour = Number(condensedVal.toString());
} else {
// colon in it
tHour = Number(condensedVal.toString().substring(0,colonMatch));
tMin = Number(condensedVal.toString().substring(colonMatch+1,condensedVal.toString().length));
}

}

alert(tHour.toString() +":"+tMin.toString());
return false;
},
"Miliatry or hour:min am/pm"
);
-->

